package com.qdu.controller;


import com.qdu.commons.RedisKey;
import com.qdu.commons.Result;
import com.qdu.entity.User;
import com.qdu.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ellery2018
 * @since 2022-03-09
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "用户操作模块")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping("/unbind")
    @ApiOperation(value = "解除齐鲁工惠绑定的微信账户")
    public Result unbind(@RequestBody User user) {
        Result result = userService.unbind(user);
        log.info("{}",result);
        return result;
    }

    @GetMapping("/getUserById")
    @ApiOperation(value = "获取用户积分")
    public Result getPoint(int Id) {
        User user =  userService.getById(Id);
        log.info("查询用户信息");
        if (Objects.isNull(user)) {
            return Result.FAIL("网络错误！");
        }
        return Result.SUCCESS(user);
    }

    @GetMapping("/getTotalRank")
    @ApiOperation(value = "获取排行榜")
    public Result getRank() {
         return userService.getTotalRank();
    }

    @GetMapping("/getMyRank")
    @ApiOperation(value = "获取我的排行榜信息")
    public Result getMyRank(int userId) {
        return userService.getMyRank(userId);
    }

    @GetMapping("/test")
    @ApiOperation(value = "给用户加积分")
    public Result test() {
        userService.completeGetPoint(9);
        return Result.SUCCESS("jay");
    }


    @PostMapping("/signforpower")
    public Result signForPower(@RequestParam("id") Integer userid ) {

//        Result result = userService.unbind(user);
        if(userService.sameDate(userid)==true){
            userService.signGetPower(userid);
            return Result.SUCCESS("ok");
        }
        else{
            return Result.FAIL("sameday");
        }

    }

    @PostMapping("/changepower")
    public Result changePower(@RequestParam("id") Integer userid ,@RequestParam("addpower") Integer addpower ) {


        if(userService.sameChangeDate(userid)==true){
            userService.updateHealthPower(userid,-addpower);
            return Result.SUCCESS("ok");
        }
        else{
            return Result.FAIL("same");
        }

    }

    @PostMapping("/questionsforpower")
    public Result questionsForPower(@RequestParam("id") Integer userid  ) {

           Integer hasanswer = userService.findAnswerNumber(userid);
        if(hasanswer<10){
            userService.updateHealthPower(userid,-40000);
            userService.addAnswerNumber(userid);
            return Result.SUCCESS("ok");
        }
        else{
            return Result.FAIL("notime");
        }

    }

    @PostMapping("/findhealthpower")
    public Result findHealthPower(@RequestParam("id") Integer userid  ) {

        return Result.SUCCESS(userService.findHealthPower(userid));

    }

}
